package com.i2bt.app.files;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.Contract;

import com.i2bt.R;
import com.i2bt.adapter.FilesSharedListAdapter;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.files.model.FilesSharedJsonHandle;
import com.i2bt.bean.FileInfoBean;
import com.i2bt.bean.FriendBean;
import com.i2bt.bean.FriendInfoBean;
import com.i2bt.bean.GetSharedBean;
import com.i2bt.util.DateFormatUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class FilesSharedActivity extends BaseActivity {
	
	private ListView filesSharedListView; // 好友分享列表视图
	
	private List<FriendInfoBean> friendSharedData; // 好友分享列表
	
	private FilesSharedListAdapter filesSharedListAdapter;
	
	private FilesSharedJsonHandle filesSharedJsonHandle; // 好友分享处理器
	
	@Override
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_files_shared);
		
		logic();
		initView();
	}

	@Override
	protected void initView() {
		filesSharedListAdapter = new FilesSharedListAdapter(this, friendSharedData);
		
		filesSharedListView = (ListView) findViewById(R.id.files_shared_list);
		filesSharedListView.setAdapter(filesSharedListAdapter);
		
		Button friendSharedBtn = (Button) findViewById(R.id.files_shared_btn);
		friendSharedBtn.setOnClickListener(this);
	}
	
	@Override
	protected void logic() {
		if (filesSharedJsonHandle == null) {
			filesSharedJsonHandle = new FilesSharedJsonHandle(this);
		}
		
		//调用获取好友列表请求
		showFriendRequest();
	}
	
	/**
	 * 获取好友列表请求
	 */
	private void showFriendRequest() {
		FriendBean friendBean = new FriendBean();
		friendBean.setAct(AndroidRestContract.SHOW_FRIEND);
		filesSharedJsonHandle.postTo(AndroidRestContract.REST_FRIEND,
				friendBean);
	}
	
	/**
	 * 获取好友列表响应
	 * @param friendSharedData  数据
	 */
	public void showFriendResponse(List<FriendInfoBean> friendSharedData) {
		this.friendSharedData = friendSharedData;
		filesSharedListAdapter.setData(friendSharedData);
	}
	
	@Override
	public void onClick(View view) {
		sharedFilesRequest();
	}
	
	private void sharedFilesRequest() {
		GetSharedBean shareBean = new GetSharedBean();
		shareBean.setAct(AndroidRestContract.ADD_MULT_SHARE);
		shareBean.setShareDate(DateFormatUtil.getDateFormat());
		shareBean.setShareList(getSharedList());
		shareBean.setSharePathList(getSharedPathList());
		filesSharedJsonHandle.postTo(AndroidRestContract.REST_SHARED, shareBean);
	}
	
	/**
	 * 获取选中文件信息, 从
	 * @return
	 */
	private FileInfoBean[] getSharedPathList() {
		Parcelable[] sharedPathParces = getIntent().
				getParcelableArrayExtra(Contract.BUNDLE_FILES_SHARED_PATH_LIST);
		FileInfoBean[] sharedPathList = new FileInfoBean[sharedPathParces.length];
		for (int i = 0; i < sharedPathParces.length; i++) {
			sharedPathList[i] = (FileInfoBean) sharedPathParces[i];
		}
		return sharedPathList;
	}
	
	/**
	 * 获取选中的分享好友字符串数组
	 * @return 选中的好友数组
	 */
	private String[] getSharedList() {
		List<Integer> friendCheckedSharedList = filesSharedListAdapter
				.getCheckedFriendSharedList();
		
		String[] sharedList = new String[friendCheckedSharedList.size()];
		for (int i = 0; i < sharedList.length; i++) {
			String friendId = friendSharedData.get(
					friendCheckedSharedList.get(i)).getFriendId();
			try {
				sharedList[i] = URLEncoder.encode(friendId, "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		return sharedList;
	}
	
	/**
	 * 文件分享响应
	 */
	public void sharedFilesResponse() {
		toastHandler("分享成功!");
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.menu = menu;
		getMenuInflater().inflate(R.menu.files_shared_menu, menu);
		setActionBarBack(Contract.TITLE_FILE_SHARED_NAME);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		
		// 全选
		case R.id.action_files_shared_alllchecked : {
			setAllChecked(true);
			break;
		}
		
		// 全不选
		case R.id.action_files_shared_allcancel : {
			setAllChecked(false);
			break;
		}
		
		}
		
		return true;
	}
	
	/**
	 * 设置文件分享的好友全选和全不选的切换
	 * @param isAllChecked
	 */
	private void setAllChecked(boolean isAllChecked) {
		// 设置所有好友全选或者全不选
		filesSharedListAdapter.setAllChecked(isAllChecked);
		
		// 设置菜单按钮全选和全不选的切换显示
		MenuItem allCheckedItem = (MenuItem) menu.findItem(R.id.action_files_shared_alllchecked);
		MenuItem allCancelItem = (MenuItem) menu.findItem(R.id.action_files_shared_allcancel);
		allCheckedItem.setVisible(!isAllChecked);
		allCancelItem.setVisible(isAllChecked);
	}
	
	@Override
	public void backHomeOperate() {
		finish();
	}
	
	
}
